package com.cheng.imagerelated.photoselecview.adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.GenericTransitionOptions;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.cheng.imagerelated.R;
import com.cheng.imagerelated.photoselecview.entity.Image;
import com.cheng.imagerelated.photoselecview.widget.SquareImageView;

import java.util.ArrayList;
import java.util.List;

public class ImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    // 确定按钮
    private TextView tvConfirm;
    // 确定按钮得盖板
    private FrameLayout btnConfirm;

    private List<Image> mList = new ArrayList<>();
    private LayoutInflater mInflater;

    // 选择最大的图片数
    private int mMaxCount;
    // 是否单选
    //保存选中的图片
    private ArrayList<Image> mSelectImages = new ArrayList<>();
    private boolean isSingle;
    //是否是显示拍照图片
    private boolean showCamera;
    // 带拍照
    private final static int ITEM_TYPE_CAMERA = 100;
    // 不带拍照
    private final static int ITEM_TYPE_PHOTO = 101;
    // 拍照点击事件监听
    private View.OnClickListener onCameraClickListener = null;

    public ImageAdapter(FrameLayout btnConfirm, TextView tvConfirm, Context context, boolean showCamera, boolean isSingle, int mMaxCount) {
        this.mContext = context;
        this.tvConfirm = tvConfirm;
        this.mInflater = LayoutInflater.from(mContext);
        this.showCamera = showCamera;
        this.isSingle = isSingle;
        this.mMaxCount = mMaxCount;
        this.btnConfirm = btnConfirm;
    }

    public void setGridDataList(List<Image> list) {
        mList = list;
        // 将已选图片存起来
        int count = null == mList ? 0 : mList.size();
        if (null != mSelectImages) {
            mSelectImages.clear();
        }
        for (int i = 0; i < count; i++) {
            Image image = mList.get(i);
            if (image.isSelect()) {
                mSelectImages.add(image);
            }
        }
        // 设置确定按钮显示
        int size = null == mSelectImages ? 0 : mSelectImages.size();
        if (size > 0) {
            tvConfirm.setText(mContext.getString(R.string.confirm_maxcount, size, mMaxCount));
            btnConfirm.setEnabled(true);
        } else {
            tvConfirm.setText(mContext.getString(R.string.confirm));
            btnConfirm.setEnabled(false);
        }
        notifyDataSetChanged();
    }

    /**
     * 获取已选中得图片
     *
     * @return
     */
    public ArrayList<Image> getmSelectImages() {
        return mSelectImages;
    }

    @Override
    public int getItemCount() {
        if (showCamera) {
            return mList == null ? 0 : mList.size() + 1;
        } else {
            return mList == null ? 0 : mList.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (showCamera && position == 0) {
            return ITEM_TYPE_CAMERA;
        } else {
            return ITEM_TYPE_PHOTO;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_CAMERA) {
            CameraHolder cameraHolder = new CameraHolder(mInflater.inflate(R.layout.adapter_camera_item, parent, false));
            cameraHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onCameraClickListener != null) {
                        onCameraClickListener.onClick(v);
                    }
                }
            });
            return cameraHolder;
        } else {
            return new ImageViewHolder(mInflater.inflate(R.layout.item_image, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {

        if (getItemViewType(position) == ITEM_TYPE_PHOTO) {
            final ImageViewHolder imageViewHolder = (ImageViewHolder) holder;
            final int mPosition;
            if (showCamera) {
                mPosition = position - 1;
            } else {
                mPosition = position;
            }

            Glide.with(mContext).load(mList.get(mPosition).getPath())
                    .transition(new GenericTransitionOptions<>().transition(android.R.anim.slide_in_left))
                    .transition(new DrawableTransitionOptions().crossFade(150))
                    .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.NONE)
                            .centerCrop()
                            .placeholder(R.drawable.ic_image)
                            .error(R.drawable.ic_img_load_fail))
                    .thumbnail(0.5f)
                    .into(imageViewHolder.imageView);


            Image image = mList.get(mPosition);
            setItemSelect(imageViewHolder, image.isSelect());

            onItemClick(imageViewHolder, mPosition);
        }
    }

    /**
     * 设置图片选中和未选中的效果
     */
    private void setItemSelect(ImageViewHolder holder, boolean isSelect) {
        if (isSelect) {
            holder.ivSelect.setImageResource(R.drawable.ic_image_select);
        } else {
            holder.ivSelect.setImageResource(R.drawable.ic_image_un_select);
        }
    }

    /**
     * item及选择得点击事件
     *
     * @param holder
     * @param position
     */
    private void onItemClick(final ImageViewHolder holder, final int position) {
        holder.ivSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isSingle) {
                    radioImage(holder, mList, position);
                } else {
                    multipleImage(holder, mList, position, mMaxCount);
                }
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mListener) {
                    mListener.onItemClick(position);
                }
            }
        });
    }

    /**
     * 图片单选
     *
     * @param holder
     * @param mList
     * @param position
     */
    private void radioImage(ImageViewHolder holder, List<Image> mList, int position) {
        Image image = mList.get(position);
        mSelectImages.clear();

        int count = null == mList ? 0 : mList.size();
        for (int i = 0; i < count; i++) {
            Image image1 = mList.get(i);
            image1.setSelect(i == position);
            if (i == position) {
                mSelectImages.add(image);
            }
        }
        holder.itemView.post(new Runnable() {
            @Override
            public void run() {
                notifyDataSetChanged();
            }
        });
    }


    /**
     * 图片多选
     *
     * @param holder
     * @param mList     // 图片源数据
     * @param position  // 当前选择中得那个
     * @param mMaxCount // 最大数
     */
    private void multipleImage(ImageViewHolder holder, List<Image> mList, int position, int mMaxCount) {

        Image image = mList.get(position);
        if (mSelectImages.contains(image)) {
            mSelectImages.remove(image);
            setItemSelect(holder, false);
        } else {
            int count = null == mSelectImages ? 0 : mSelectImages.size();
            // 限制最大数
            if (count < mMaxCount) {
                setItemSelect(holder, true);
                mSelectImages.add(image);
            }
        }
        // 设置确定按钮显示
        int size = null == mSelectImages ? 0 : mSelectImages.size();
        if (size > 0) {
            tvConfirm.setText(mContext.getString(R.string.confirm_maxcount, size, mMaxCount));
            btnConfirm.setEnabled(true);
        } else {
            tvConfirm.setText(mContext.getString(R.string.confirm));
            btnConfirm.setEnabled(false);
        }
    }

    public class ImageViewHolder extends RecyclerView.ViewHolder {
        SquareImageView imageView;
        ImageView ivSelect;

        public ImageViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv_image);
            ivSelect = itemView.findViewById(R.id.iv_select);
        }
    }

    class CameraHolder extends RecyclerView.ViewHolder {

        ImageView ivCamera;

        CameraHolder(View itemView) {
            super(itemView);
            ivCamera = itemView.findViewById(R.id.iv_camera);
        }
    }

    private OnItemClickListener mListener;

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }

    /**
     * 拍照监听
     *
     * @param onCameraClickListener
     */
    public void setOnCameraClickListener(View.OnClickListener onCameraClickListener) {
        this.onCameraClickListener = onCameraClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int content);
    }
}
